package com.hee.erp.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hee.erp.filter.LoginContextHolder;
import com.hee.erp.modules.entity.SysRight;
import com.hee.erp.modules.entity.SysUser2Right;
import com.hee.erp.modules.mapper.SysRightMapper;
import com.hee.erp.modules.service.ISysRightService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.pojo.SysRightTreeNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-03-04
 */
@Service
@Transactional
public class SysRightServiceImpl extends ServiceImpl<SysRightMapper, SysRight> implements ISysRightService {


    @Override
    public List<SysRightTreeNode> getMenuRightsByIds(List<String> rightIds) {
        LambdaQueryWrapper<SysRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysRight::getId,rightIds);
        queryWrapper.eq(SysRight::getType,0);
        queryWrapper.orderByAsc(SysRight::getSortNo);
        List<SysRight> rights = this.list(queryWrapper);
        List<SysRightTreeNode> treeNodes = createTreeNodes(rights, "0");
        return treeNodes;
    }

    @Override
    public List<SysRightTreeNode> getAllRights() {
        LambdaQueryWrapper<SysRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysRight::getSortNo);
        List<SysRight> list = this.list(queryWrapper);
        List<SysRightTreeNode> treeNodes = createTreeNodes(list, "0");
        return treeNodes;
    }

    @Override
    public IPage<SysRight> listByPage(JSONObject params) {

        Integer pageSize = params.getInteger("pageSize");
        Integer current = params.getInteger("current");
        IPage<SysRight> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<SysRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRight::getParentId,"0");
        queryWrapper.orderByAsc(SysRight::getSortNo);

        this.page(page,queryWrapper);

        List<SysRight> records = page.getRecords();
        setChildren(records);

        return page;
    }

    public void setChildren(List<SysRight> list){
        for (SysRight record : list) {
            List<SysRight> children = this.listByParentId(record.getId());
            if(children != null && children.size() > 0){
                for (SysRight child : children) {
                    child.setParentName(record.getShowName());
                }
                record.setChildren(children);
                setChildren(children);
            }
        }
    }

    public List<SysRight> listByParentId(String parentId){
        LambdaQueryWrapper<SysRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRight::getParentId,parentId);
        queryWrapper.orderByAsc(SysRight::getSortNo);

        List<SysRight> list = this.list(queryWrapper);
        return list;
    }


    public List<SysRightTreeNode> createTreeNodes(List<SysRight> rights, String parentId){

        List<SysRight> rightList = rights.stream().filter(item -> item.getParentId().equals(parentId)).collect(Collectors.toList());
        if(rightList.size() == 0){
            return null;
        }else {
            List<SysRightTreeNode> treeNodes = new ArrayList<>();
            for (SysRight right : rightList) {
                SysRightTreeNode node = new SysRightTreeNode();
                BeanUtils.copyProperties(right, node);
                node.setChildren(createTreeNodes(rights,right.getId()));
                treeNodes.add(node);
            }
            return treeNodes;
        }
    }
}
